#!/usr/local/bin/ruby
require 'rubygems'
require 'open-uri'
require 'uri'
require 'net/http'
require 'xmlsimple'

   def escape(s); s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n){'%'+$1.unpack('H2'*$1.size).join('%').upcase}.tr(' ', '+') end


puts '<?xml version="1.0" encoding="UTF-8"?>'
puts '<feeds xmlns="http://beta.meadan.org/doctypes/feeds">'
count = 0 

10.times do 

  start = count*100+1
  count = count + 1 
  url = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=anselmhook&query=middle%20east%20news&format=rss&results=100&start=#{start}"

  feed = Net::HTTP.get_response(URI.parse(url)).body
  #feed = File.open("test.xml").read
  data = XmlSimple.xml_in(feed)
  data['Result'].each do |e|
    l = "#{e['feedUrl'].last}"

    l = l.gsub(/&/,'&amp;')
    l = l.gsub(/</,'&lt;')
    l = l.gsub(/>/,'&gt;')
    
    puts "<feed url="+'"'+"#{l}"+'"'+" />"

  end
end

puts "</feeds>"

